package cn.com.jrr.core.freemarker;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Freemaker {
	@RequestMapping("/autoCreate")
	public String autoCreate(){
		return "/freemarker/freemarker";
	}

	@RequestMapping("/freemarker")
	public String freemarker(HttpServletRequest request,String fieldName[],String className){
		System.out.println(fieldName.length);
		String projectName=request.getContextPath();
		Map<String,Object> map=new HashMap<String, Object>();
		Map<String,String> fieldMap=null;
		List<Map<String,String>> properties=new ArrayList<>();
		for(int i=0;i<fieldName.length;i++){
			fieldMap=new HashMap<String, String>();
			fieldMap.put("fieldName", fieldName[i]);
			System.out.println(fieldName[i]);
			properties.add(fieldMap);
		}

		map.put("properties", properties);
		map.put("className", className);
		map.put("lowerClassName", this.firstUpperCase(className));
		map.put("projectName", projectName);
		FreemarkerUtil fu=new FreemarkerUtil();
		//生成model文件
		//fu.fprint("model.ftl", map, "D:/Maven/TEST/src/cn/com/jrr/model/"+this.firstUpperCase(className)+".java");
		//生成service文件
		//fu.fprint("service.ftl", map, "D:/Maven/TEST/src/cn/com/jrr/service/"+this.firstUpperCase(className)+"Service.java");
		//生成controller文件
		fu.fprint("controller.ftl", map, "D:/Maven/TEST/src/cn/com/jrr/controller/"+this.firstUpperCase(className)+"Controller.java");
		//创建相应的目录
		createDir("D:/Maven/TEST/WebContent/WEB-INF/views/"+className);
		createDir("D:/Maven/TEST/WebContent/static/project/"+className);
		//生成jsp文件
		fu.fprint("webJsp.ftl", map, "D:/Maven/TEST/WebContent/WEB-INF/views/"+className+"/"+className+".jsp");
		//生成js文件
		fu.fprint("webJs.ftl", map, "D:/Maven/TEST/WebContent/static/project/"+className+"/"+className+".js");
		return "index";
	}
	@RequestMapping("/tt")
	public String freemark(HttpServletRequest request){
		System.out.println(System.getProperty("user.dir"));
		return "index";
	}

	/**
	 * 字符串首字母大写
	 *
	 * @param name
	 * @return
	 * String
	 */
	public String firstUpperCase(String name) {
		char[] cs=name.toCharArray();
		cs[0]-=32;
		return String.valueOf(cs);
	}
	/**
	 * 创建目录
	 *
	 * @param name
	 * @return
	 * String
	 */
	public boolean createDir(String destDirName) {
		File dir = new File(destDirName);
		if (dir.exists()) {
			System.out.println("创建目录" + destDirName + "失败，目标目录已经存在");
			return false;
		}
		if (!destDirName.endsWith(File.separator)) {
			destDirName = destDirName + File.separator;
		}
		//创建目录
		if (dir.mkdirs()) {
			System.out.println("创建目录" + destDirName + "成功！");
			return true;
		} else {
			System.out.println("创建目录" + destDirName + "失败！");
			return false;
		}
	}
}
